package cn.iocoder.yudao.module.wine.service.advertisement;

import cn.iocoder.yudao.module.wine.controller.app.advertisement.vo.AppAdvertisementPageReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.advertisement.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.advertisement.AdvertisementDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.advertisement.AdvertisementMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 广告信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class AdvertisementServiceImpl implements AdvertisementService {

    @Resource
    private AdvertisementMapper advertisementMapper;

    @Override
    public Long createAdvertisement(AdvertisementSaveReqVO createReqVO) {
        // 插入
        AdvertisementDO advertisement = BeanUtils.toBean(createReqVO, AdvertisementDO.class);
        advertisementMapper.insert(advertisement);
        // 返回
        return advertisement.getId();
    }

    @Override
    public void updateAdvertisement(AdvertisementSaveReqVO updateReqVO) {
        // 校验存在
        validateAdvertisementExists(updateReqVO.getId());
        // 更新
        AdvertisementDO updateObj = BeanUtils.toBean(updateReqVO, AdvertisementDO.class);
        advertisementMapper.updateById(updateObj);
    }

    @Override
    public void deleteAdvertisement(Long id) {
        // 校验存在
        validateAdvertisementExists(id);
        // 删除
        advertisementMapper.deleteById(id);
    }

    private void validateAdvertisementExists(Long id) {
        if (advertisementMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public AdvertisementDO getAdvertisement(Long id) {
        return advertisementMapper.selectById(id);
    }

    @Override
    public PageResult<AdvertisementDO> getAdvertisementPage(AdvertisementPageReqVO pageReqVO) {
        return advertisementMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AdvertisementDO> getAdvertisementList(AppAdvertisementPageReqVO pageReqVO) {
        return advertisementMapper.getAdvertisementList(pageReqVO);
    }

}
